package spe.ui.command.handler;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

import spe.model.notation.SPEMDiagram;
import spe.model.spem.Activity;
import spe.model.spem.MethodConfiguration;
import spe.model.spem.MethodLibrary;
import spe.model.spem.MethodPlugin;
import spe.model.spem.NodeElement;
import spe.model.spem.RoleDefinition;
import spe.model.spem.SPEMFactory;
import spe.model.spem.SPEMFactoryImpl;
import spe.model.spem.TaskDefinition;
import spe.ui.editor.SPEMEditorInput;
import spe.ui.editor.SPEMGraphEditor;
import spe.ui.view.MethodLibraryView;
import spe.xml.SPEDiagramAdapter;

/*
 * Author: Dao Anh Vu
 * Last update: Jan 25, 2013
 * 
 * When user double click on an item in SPE navigation view, a double-click handler
 * will be created and invoked.
 * 
 * see class spe.ui.view.SPENavigator
 */

public class DoubleClickOnItemHandler extends AbstractHandler 
{
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		IWorkbenchPage page = window.getActivePage();
		MethodLibraryView view = (MethodLibraryView)page.findView(MethodLibraryView.VIEW_ID);
		ISelection selection = view.getSite().getSelectionProvider().getSelection();
		if( (selection != null) && ( selection instanceof IStructuredSelection) ) {
			Object obj = ((IStructuredSelection)selection).getFirstElement();
			if(obj != null)	{
				try {
					/* User double-click on diagram object */
					if(obj instanceof SPEMDiagram) {
						SPEMDiagram diagram = (SPEMDiagram) obj;
						page.openEditor(new SPEMEditorInput(null, diagram), SPEMGraphEditor.ID, false);
					} else if(obj instanceof MethodLibrary){
						
					}else if(obj instanceof MethodPlugin){
						
					}
				} catch (PartInitException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

}
